package edu.hz.dao;

import edu.hz.model.dto.HomeMostContributePeopleDto;
import edu.hz.model.entity.UserEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserMapper {

    @Select("select * from users where id = #{id}")
    UserEntity selectById(@Param("id") Integer id);

    @Select("select * from users")
    List<UserEntity> allUsers();

    @Select("select * from users where username = #{username}")
    UserEntity getUserByUsername(@Param("username") String username);

    @Insert("INSERT INTO users (username, pwd, pwd_salt, realname, gender, grade_id, clazz_id, money, title, err_login_count, school_id)" +
            "VALUES (#{username}, #{pwd}, #{pwdSalt}, #{realname}, #{gender}, #{gradeId}, #{clazzId}, #{money}, #{title}, #{errLoginCount}, #{schoolId})")
    int insertUser(UserEntity user);

    @Update("UPDATE users SET err_login_count = #{errCount} WHERE id = #{id}")
    int updateErrLoginCount(@Param("id") Integer id, @Param("errCount") Integer errLoginCount);


    @Select("select u.id, u.username, u.title, SUM(ud.money) as totalDonationAmount, count(DISTINCT ud.donation_id) as donationProjectCount "
    + " from users u left join user_donations ud on u.id = ud.user_id "
    + " group by u.id, u.username, u.title "
    + " order by totalDonationAmount desc "
    + " limit #{count}")
    List<HomeMostContributePeopleDto> mostContributePeople(@Param("count") Integer count);

}
